<template>
    <a-card
      title="新增商品"
      :maskClosable="false"
      width=1150
      placement="right"
      :closable="false"
      @close="onClose"
      :visible="maaAddVisiable"
      style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
      <a-form :form="form">
        <a-tabs defaultActiveKey="1">
          <!--商品信息-->
          <a-tab-pane tab="商品信息" key="1">
              <merchandise-news :newsData.sync="dataAll.maaFile" :selectedOption.sync="selectedOptions"></merchandise-news>
          </a-tab-pane>
          <!--图片与视频-->
          <a-tab-pane tab="图片与视频" key="2" forceRender>
            <img-video :imgData.sync="dataAll.maaFile" :fileListFF.sync="dataAll.fileListF" :fileListMM.sync="dataAll.fileListM" :fileListVVideo.sync="dataAll.fileListVideo"></img-video>
          </a-tab-pane>
          <!--其他信息-->
          <a-tab-pane tab="其他信息" key="3">
            <others-news :othersData.sync="dataAll.maaFile"></others-news>
          </a-tab-pane>
          <!--子产品信息-->
          <a-tab-pane tab="子产品信息" key="4">
            <child-product :productData.sync="dataAll.dataSourceMabChild" :mab1.sync="mab01"></child-product>
          </a-tab-pane>
        </a-tabs>
      </a-form>
      <div class="drawer-bootom-button">
        <a-popconfirm title="确定放弃编辑？" @confirm="onClose" okText="确定" cancelText="取消">
          <a-button style="margin-right: .8rem">取消</a-button>
        </a-popconfirm>
        <a-button @click="handleSubmit" type="primary" :loading="loading">提交</a-button>
      </div>
    </a-card>
</template>
<script>

import db from 'utils/localstorage'
import GlobalVariable from '../../common/global_variable'
import MerchandiseNews from './components/News'
import ImgVideo from './components/Img'
import OthersNews from './components/Others'
import ChildProduct from './components/Product'
let user = db.get('USER')
let username = user.username
export default {
  components: {
    MerchandiseNews,
    ChildProduct,
    ImgVideo,
    OthersNews
  },
  name: 'maaAdds',
  props: {
    maaAddVisiable: {
      default: false
    }
  },
  data () {
    return {
      selectedOptions: [],
      mab01: '',
      maa01: '',
      dataAll: {
        maaFile: {
          maa00: '',
          maa01: '',
          maa02: '',
          maa03: '',
          maa04: '',
          maa05: '',
          maa06: '',
          maa10: '',
          maa11: '0',
          maa12: '',
          maa13: '',
          maa14: '',
          maa15: '',
          maa16: '',
          maa17: '',
          maa19: '',
          maa20: '',
          maa30: '0',
          maa35: '',
          maa36: '',
          maa37: '',
          maa38: '',
          maa39: '',
          maa40: '',
          maa41: '',
          maaacti: 'N'
        },
        fileListF: [],
        fileListM: [],
        fileListVideo: [],
        dataSourceMabChild: []
      },
      thumbUrl: '',
      fileKeys: '',
      actionUrl: GlobalVariable.preRequest + 'file/uploadQCloud',
      loading: false,
      form: this.$form.createForm(this),
      checkedKeys: [],
      expandedKeys: [],
      sortedInfoMab: null,
      paginationMab: {
        defaultPageSize: 10000000,
        hideOnSinglePage: true,
        indentSize: 100
      },
      loadingMab: false,
      queryParamsMabChild: {},
      sortedInfoMabChild: null,
      paginationMabChild: {
        defaultPageSize: 10000000,
        hideOnSinglePage: true,
        indentSize: 100
      }
    }
  },
  methods: {
    // 重置
    reset () {
      this.expandedKeys = this.checkedKeys = []
      this.selectedOptions = []
      this.dataAll.fileListF.splice(this.dataAll.fileListF.indexOf(0), 1)
      this.dataAll.fileListM.splice(this.dataAll.fileListM.indexOf(0), 1)
      this.dataAll.fileListVideo.splice(this.dataAll.fileListVideo.indexOf(0), 1)
      let obj = this.dataAll.maaFile
      this.maa01 = obj.maa01
      for (let j in obj) {
        obj[j] = ''
      }
      obj.maa01 = this.maa01
      return obj
      this.form.resetFields()
      this.loading = false
    },
    // 取消
    onClose () {
      this.reset()
      this.$emit('close')
    },
    // 提交
    handleSubmit () {
      this.form.validateFields((err, values) => {
        console.log(this.dataAll)
        if (this.dataAll.maaFile.maa17 === '') {
          this.$message.error('请上传产品主图')
          return false
        }
        if (!err) {
          this.loading = true
          // 提交商品信息
          console.log(this.dataAll)
          this.$api.goods.postMaa(this.dataAll.maaFile).then((r) => {
            this.reset()
            this.$emit('success')
            this.$message.success('新增商品成功')
            this.$router.push({path: '/goods/maa/maa'})
          }).catch(() => {
            this.loading = false
          })
        }
      })
    }
  },
  beforeCreate () {
    this.$api.goods.getSKU().then((r) => {
      this.dataAll.maaFile.maa01 = r.data.data
      this.mab01 = this.dataAll.maaFile.maa01
    })
  }
}
</script>
